package com.yanqu.road.server.cmd.activity.hsisland;

import com.yanqu.road.entity.activity.hsisland.data.HsIslandPatronsData;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiYNEnum;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.hsisland.*;
import com.yanqu.road.server.manager.activity.hsisland.pb.CrossHsIslandActivityPb;
import com.yanqu.road.server.manager.activity.hsisland.rank.CrossHsIslandRankListModel;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;

import java.util.List;

import static com.yanqu.road.entity.activity.hsisland.enums.HsIslandBattleFieldEnemyEnum.ENEMY_BOSS;

@Cmd(code = Protocol.C2_HS_ISLAND_ENTER_ACTIVITY, desc = "海神岛-进入活动")
public class CrossHsIslandEnterActivityCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        HsIslandProto.HsIslandEnterActivityCrossReqMsg reqMsg = HsIslandProto.HsIslandEnterActivityCrossReqMsg.parseFrom(packet.getBytes());
        HsIslandProto.HsIslandEnterActivityRespMsg.Builder respMsg = HsIslandProto.HsIslandEnterActivityRespMsg.newBuilder();
        CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_ENTER_ACTIVITY, respMsg));
            return;
        }
        //进入活动
        try {
            //存储userBase
            if(reqMsg.hasUserInfo()){
                UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
                CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
            }
            //解析出门客LIST
            List<HsIslandPatronsData> patronsList = CrossHsIslandActivityPb.parsePatronsList(reqMsg.getPatronsList());
            //进入活动
            activity.enterActivity(packet.getPlayerId(), patronsList);
            //同步下红点
            activity.syncUserRedDot(packet.getPlayerId());
            //构造PB
            CrossHsIslandUser user = activity.getUser(packet.getPlayerId());
            CrossHsIslandFloor floor = user.getFloor();
            respMsg.setRet(0);
            respMsg.setUserData(CrossHsIslandActivityPb.buildHsIslandUserData(user));
            respMsg.setFloor(CrossHsIslandActivityPb.buildHsIslandFloorData(floor));
            respMsg.setMaxBattleValue(activity.getActivityData().getMaxBattleValue());
            respMsg.setBattleValue(activity.getActivityData().getBattleValue());
            respMsg.setBossNum(activity.getActivityData().getBossNum());
            // 当前击杀的boss信息
            for (CrossHsIslandKillBossRecord killBossRecord : activity.getKillBossRecordMap().values()) {
                HsIslandProto.HsIslandKillBossRecordTemp.Builder killBossTemp = activity.parseHsIslandKillBossRecordTemp(killBossRecord, user);
                if (killBossTemp != null) {
                    respMsg.addKillBossRecord(killBossTemp);
                }
            }
            if (activity.getActivityData().getBossData() != null) {
                respMsg.setBossData(CrossHsIslandActivityPb.buildHsIslandMonsterData(activityId, groupId, activity.getActivityData().getBossData(), false));
            }
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_ENTER_ACTIVITY, respMsg));
    }

    @Override
    public void sendData() {

    }
}
